local function Size(table)
	local size = 0;
	for index,value in pairs(table) do
		size = size +1;
	end
	return size;
end
local inventoryMap = redis.call('HGETALL',KEYS[1]);
local size = Size(inventoryMap);
if size==0 or redis.call('EXISTS',KEYS[2]) == 1 then
	return -1;
end
for i = 1,size,2 do
	local goodsClass = inventoryMap[i];
	local inventory = inventoryMap[i+1];
	local buyGoodsClass = string.sub(ARGV[1],2,string.len(ARGV[1])-1);
	if goodsClass == buyGoodsClass  then
		local surplus = inventory - ARGV[2];
		if surplus >= 0 then
		    redis.call('HSET',KEYS[1],goodsClass,surplus);
			return surplus;
		else
			return -3;
		end
	end
end
return -2;